/**
 * 返回字符串的实际长度, 一个汉字算2个长度
 * @returns {*}
 */
export let strLen = str => {
  if (typeof str !== 'string') return null
  return str.replace(/[^\x00-\xff]/g, '**').length
}

/**
 * 字符串超出省略
 * @param str
 * @param len
 * @returns {*}
 * @private
 */
export const cutStr = (str, len) => {
  if (typeof str !== 'string') return null
  if (typeof len !== 'number') return null
  let restr = str
  let wlength = str.replace(/[^\x00-\xff]/g, '**').length
  if (wlength > len) {
    for (let k = len / 2; k < str.length; k++) {
      if (str.substr(0, k).replace(/[^\x00-\xff]/g, '**').length >= len) {
        restr = str.substr(0, k) + '..'
        break
      }
    }
  }
  return restr
}
/**
 * 替换全部
 * @param str
 * @param s1
 * @param s2
 * @returns {*}
 */
export let replaceAll = (str, s1, s2) => {
  if (typeof str !== 'string') return null
  if (typeof s1 !== 'string') return null
  if (typeof s2 !== 'string') return null
  return this.replace(new RegExp(s1, 'gm'), s2)
}

/**
 * 字符串循环复制
 * @param str
 * @param n
 * @returns {*}
 */
export let strRepeat = (str, n) => {
  if (typeof str !== 'string') return null
  if (typeof n !== 'number') return null
  return str.repeat(n)
}

/**
 * 字符串去空格
 * @returns {*}
 */
export let trim = target => {
  if (typeof target !== 'string') return null
  return target.replace(/(^\s*)|(\s*$)/g, '')
}

export let trimAll = target => {
  if (typeof target !== 'string') return null
  return target.replace(/\s+/g, '')
}

/**
 * 字符串去左边的空格
 * @returns {*}
 */
export let trimLeft = target => {
  if (typeof target !== 'string') return null
  return target.replace(/(^\s*)/g, '')
}
/**
 * 字符串去右边的空格
 * @returns {*}
 */
export let trimRight = target => {
  if (typeof target !== 'string') return null
  return target.replace(/(\s*$)/g, '')
}

/**
 * 判断是否以某个字符串开头
 * @param target
 * @param str
 * @returns {boolean}
 */
export let startWith = (target, str) => {
  if (typeof target !== 'string') return null
  if (typeof str !== 'string') return null
  return target.indexOf(str) === 0
}

/**
 * 判断是否以某个字符串结束
 * @param target
 * @param str
 * @returns {boolean}
 */
export let endWith = (target, str) => {
  if (typeof target !== 'string') return null
  if (typeof str !== 'string') return null
  let d = target.length - str.length
  return d >= 0 && target.lastIndexOf(str) === d
}

/**
 * 判断是否包含字符串
 * @param target
 * @param str
 * @returns {boolean}
 */
export let strWith = (target, str) => {
  if (typeof target !== 'string') return null
  if (typeof str !== 'string') return null
  return target.indexOf(str) >= 0
}
/**
 * 加密字符串
 * @param str 待处理的字符串
 * @param souwei 要加密的字符串的位置
 * @param ARepText 加密显示的字符，默认是‘*’
 * @returns {boolean}
 * strEncrypt('18819233362','3,7','+')//位置3-7的统一被替换成+
 */
export let strEncrypt = (str, souwei, ARepText = '*') => {
  if (typeof str !== 'string') return null
  if (typeof souwei !== 'string') return null
  if (typeof ARepText !== 'string') return null
  let regText,
    Reg,
    regIndex = souwei.split(','),
    replaceText = ARepText
  //repeatStr是在上面定义过的（字符串循环复制），大家注意哦
  regIndex = regIndex.map(item => Number(item))
  regText = '(\\w{' + regIndex[0] + '})\\w{' + (1 + regIndex[1] - regIndex[0]) + '}'
  Reg = new RegExp(regText)
  let replaceCount = strRepeat(replaceText, 1 + regIndex[1] - regIndex[0])
  return str.replace(Reg, '$1' + replaceCount)
}
/**
 * 检测密码强度
 * @param str 待检测的字符串
 * @returns {number}强度等级
 */
export let pwdLevel = str => {
  if (typeof str !== 'string') return null
  let nowLv = 0
  if (str.length < 6) {
    return nowLv
  }
  let rules = [/[0-9]/, /[a-z]/, /[A-Z]/, /[\.|-|_]/]
  for (let i = 0; i < rules.length; i++) {
    if (rules[i].test(str)) {
      nowLv++
    }
  }
  return nowLv
}
/**
 * -随机码
 * @param count 取值范围2-36,2代表2进制
 * @returns {string}
 */
export let randomWord = (count = 36) => {
  if (count < 2 || count > 36) return null
  return Math.random()
    .toString(count)
    .substring(2)
}

/**
 * 统计特定字符串的次数
 * @param str 待检测的字符串
 * @param strSplit ,包含的字符串
 * @returns {number}
 */
export let countStr = (str, strSplit) => {
  if (typeof str !== 'string') return null
  if (typeof strSplit !== 'string') return null
  return str.split(strSplit).length - 1
}

/**
 * @description 过滤字符串的html标签
 * param {String} str-待处理的字符串
 * param {String} restr-替换成的字符，默认''
 */
export let filterHtml = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(/<\/?[^>]*>/g, replaceStr)
}
/**
 * @description 过滤字符串的表情
 */
export let filterEmjoy = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(
    /[^\u4e00-\u9fa5|\u0000-\u00ff|\u3002|\uFF1F|\uFF01|\uff0c|\u3001|\uff1b|\uff1a|\u3008-\u300f|\u2018|\u2019|\u201c|\u201d|\uff08|\uff09|\u2014|\u2026|\u2013|\uff0e]/gi,
    replaceStr
  )
}
/**
 * @description 过滤字符串的大写字母
 */
export let filterWordUpper = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(/[A-Z]/g, replaceStr)
}
/**
 * @description 过滤字符串的小写字母
 */
export let filterWordLower = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(/[a-z]/g, replaceStr)
}
/**
 * @description 过滤字符串的数字
 */
export let filterNumber = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(/[1-9]/g, replaceStr)
}
/**
 * @description 过滤字符串的中文
 */
export let filterChinese = (str, replaceStr = '') => {
  if (typeof str !== 'string') return null
  if (typeof replaceStr !== 'string') return null
  return str.replace(/[\u4E00-\u9FA5]/g, replaceStr)
}

/**
 * @description 格式化处理字符串
 * formatText('1234asda567asd890',4,'-')
 * result："1-234a-sda5-67as-d890"
 */
export let formatStr = (str, size = 3, delimiter = ',') => {
  if (typeof str !== 'string') return null
  if (typeof size !== 'number') return null
  if (typeof delimiter !== 'string') return null
  let regText = '\\B(?=(\\w{' + size + '})+(?!\\w))'
  let reg = new RegExp(regText, 'g')
  return str.replace(reg, delimiter)
}
/**
 * 验证是否为手机
 * @param mobile
 * @return {boolean}
 */
export let isMobile = mobile => /^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile)
/**
 * @description 现金额大写转换函数
 * @param n
 * @return {string}
 */
export let upDigit = n => {
  if (typeof n !== 'number') return null
  let fraction = ['角', '分', '厘']
  let digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  let unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']]
  let head = n < 0 ? '欠人民币' : '人民币'
  n = Math.abs(n)
  let s = ''
  for (let i = 0; i < fraction.length; i++) {
    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
  }
  s = s || '整'
  n = Math.floor(n)
  for (let i = 0; i < unit[0].length && n > 0; i++) {
    let p = ''
    for (let j = 0; j < unit[1].length && n > 0; j++) {
      p = digit[n % 10] + unit[1][j] + p
      n = Math.floor(n / 10)
    }
    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
    //s = p + unit[0][i] + s;
  }
  return (
    head +
    s
      .replace(/(零.)*零元/, '元')
      .replace(/(零.)+/g, '零')
      .replace(/^整$/, '零元整')
  )
}
